﻿CREATE PROCEDURE [Admin].[sp_z4s_encrypt_data]
    @decryptedValue nvarchar(MAX),
    @idValue uniqueidentifier,
    @keyPassword nvarchar(255),
    @encryptedValue varbinary(MAX) OUTPUT
AS
BEGIN

    -- Open symmetric key
    Declare @openKey nvarchar(MAX)
    set @openKey='
    Open Symmetric Key Zen4SyncSymmetricKey
    decryption By password ='''+@keyPassword+''''
    
    exec [dbo].[sp_executesql] @openKey
 
    --Encryption of the decrypted value
    select @encryptedValue = EncryptByKey( key_guid('Zen4SyncSymmetricKey'), @decryptedValue, 1, CONVERT(nvarchar(36),@idValue))

    Close Symmetric Key Zen4SyncSymmetricKey

    -- Return the result of the function
    RETURN 0

END